package cn.calm.xhtml.impl.ui;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

import cn.calm.xhtml.annotation.Attribute;
import cn.calm.xhtml.annotation.Tag;
import cn.calm.xhtml.api.exception.XhtmlException;
import cn.calm.xhtml.api.service.AbstraceNodeProcess;
import cn.calm.xhtml.api.service.ResponseWriter;
import cn.calm.xhtml.api.service.XhtmlContext;
@Tag(namespace = "http://www.calm.cn/xhtml/extras/ui", attributes = {
		@Attribute(name = "items", description = "迭代对象", template = "<label></label><input name=\"items\"/>"),
		@Attribute(name = "var", description = "当前对象", template = "<label></label><input name=\"var\"/>"),
		@Attribute(name = "childrenProperty", description = "子列表", template = "<label></label><input name=\"textProperty\"/>") })
public class Tree extends AbstraceNodeProcess {
	public static final String KEYS_ITEMS = "items";
	public static final String KEYS_CHILDREN = "childrenProperty";
	public static final String KEYS_VAR = "var";
	private Method method;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void processNode(XhtmlContext context) throws XhtmlException {
		Object value = getAttrValue(KEYS_ITEMS, context);

		String var = getAttrValue(KEYS_VAR, context).toString();
		
		String childrenProperty = getAttrValue(KEYS_CHILDREN, context).toString();
		
		Iterable<?> ite = null;
		if (value instanceof Iterable) {
			ite = (Iterable<?>) value;
		} else if (value instanceof Object[]) {
			ite = Arrays.asList((Object[]) value);
		} else {
			throw new XhtmlException(KEYS_ITEMS + " is not Iterable");
		}
		
		ResponseWriter writer = context.getResponseWriter();
		writer.startTag("div");
		writer.writeAttr("class", "tree-con");
		writer.write(">");
		
		process(context,ite,var,childrenProperty);
		
		writer.endTag("div");
	}
	private void process(XhtmlContext context,Iterable<?> ite,String var,String childrenProperty ){
		ResponseWriter writer = context.getResponseWriter();
		for (Object o : ite) {
			if(method==null){
				Class<? extends Object> clazz = o.getClass();
				try {
					method = clazz.getMethod("get"+childrenProperty.substring(0, 1).toUpperCase()+childrenProperty.substring(1));
				} catch (NoSuchMethodException | SecurityException e) {
					e.printStackTrace();
				}
			}
			writer.startTag("ul");
			writer.writeAttr("class", "tree-level");
			writer.write(">");
			context.setValue(var, o);
			writer.startTag("li>");
			processChildren(context);
			writer.endTag("li");
			
			try {
				Object object = method.invoke(o);
				
				Collection<?> convertCollection = convertCollection(object);
				if(!convertCollection.isEmpty()){
					writer.startTag("li>");
					process(context,convertCollection,var,childrenProperty);
					writer.endTag("li");
				}
			} catch (IllegalAccessException | IllegalArgumentException
					| InvocationTargetException e) {
				e.printStackTrace();
			}
			
			writer.endTag("ul");
		}
	}
	
	
}
